package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IChannelDao;
import com.magicode.cms.dao.IEmailContactDao;
import com.magicode.cms.dao.ITemplateDao;
import com.magicode.cms.entiy.base.Channel;
import com.magicode.cms.entiy.base.EmailContact;
import com.magicode.cms.entiy.base.Template;
import com.magicode.cms.service.IChannelService;
import com.magicode.cms.service.IEmailContactService;
import com.magicode.cms.service.ITemplateService;

@Service("serviceEmailContact")
public class EmailContactServiceImpl extends BaseServiceImpl<EmailContact,IEmailContactDao> implements IEmailContactService,UtilPath{

	@Autowired
	@Qualifier("daoEmailContact")
	private IEmailContactDao emailContactDao;
	public IEmailContactDao getDao() throws Exception {
		return emailContactDao;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "email");
		map.put(json_title, "邮件地址");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "author");
		map.put(json_title, "作者");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 50);
		beaninfo.add(map);
		
		return beaninfo;
	}
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("email", ""+entity_alias_name+".email as email ");
			gridsel.put("author", " u.userName as author ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.EmailContact.class.getName()+" t"
				+ " left join t.author as u " 
				+ " where 1 = 1  and t.valid=1  ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_article_id)){
				hql = hql+" ";
			}
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+map.get(field_id)+"'";
			}

			if(map.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
			}
			
			if(map.containsKey("Title")){
				hql = hql+" and "+entity_alias_name+".title = '"+map.get("Title")+"'";
			}
			System.out.println(hql);
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += "  order by "+entity_alias_name+".orderCode asc "; 
			hql += "  ,"+entity_alias_name+".createTime desc "; 
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("email", ""+entity_alias_name+".email as email ");
			gridsel.put("author", " u.userName as author ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}

	@Override
	public EmailContact getByTitle(String title) throws Exception {
		Map map=new HashMap();
		map.put("Title", title);
		EmailContact channel=(EmailContact) emailContactDao.getCommonDaoService().getByHQL("select t "+this.getHql(map), null);
		return channel;
	}
	public IEmailContactDao getEmailContactDao() {
		return emailContactDao;
	}
	public void setEmailContactDao(IEmailContactDao emailContactDao) {
		this.emailContactDao = emailContactDao;
	}
	

}
